import 'package:flutter/material.dart';
import 'package:lib_uikit/dimen/gt_dimensions_v3.dart';
import 'package:lib_uikit/lib_uikit.dart';
import 'package:lib_uikit/resource/resources.dart';
import 'package:lib_uikit/widgets/gt_dropdown_v3.dart';

class GTStrokeDropdownWidgetV3 extends StatefulWidget {
  final GTStrokeDropdownSizeV3? size;
  final Widget? startWidget;
  final String text;
  final Color? textColor;
  final String? endText;
  final bool? popupShowing;
  final bool? fullWidth;
  final IconData? startIcon;
  final bool? isEnabled;
  final String? title;
  final String? bottomText;

  ///展示Title红色星号
  final bool? showTitleMarket;

  ///标题左边币种图片URL
  final String? titleCoinUrl;
  const GTStrokeDropdownWidgetV3({
    super.key,
    required this.text,
    this.textColor,
    this.size,
    this.startWidget,
    this.endText,
    this.popupShowing,
    this.fullWidth,
    this.startIcon,
    this.isEnabled,
    this.title,
    this.bottomText,
    this.showTitleMarket,
    this.titleCoinUrl,
  });

  @override
  State<StatefulWidget> createState() => _DropdownStrokeWidgetV3();
}

class _DropdownStrokeWidgetV3 extends State<GTStrokeDropdownWidgetV3> {
  late GTStrokeDropdownSizeV3 _size;
  late double height, iconSize, arrowSize;
  double radius = 6, space = 8, titleSize = 14;
  Color iconColor = GTColorsV3.icon1.color;

  @override
  void initState() {
    super.initState();
    _size = widget.size ?? GTStrokeDropdownSizeV3.size40;
    switch (_size) {
      case GTStrokeDropdownSizeV3.size44:
        height = 44;
        iconSize = 20;
        break;
      case GTStrokeDropdownSizeV3.size40:
        height = 40;
        iconSize = 16;
        break;
      case GTStrokeDropdownSizeV3.size30:
        height = 30;
        iconSize = 14;
        break;
      case GTStrokeDropdownSizeV3.size24:
        radius = 4;
        height = 24;
        iconSize = 12;
        space = 4;
        titleSize = 13;
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    iconColor = (widget.isEnabled ?? true) ? GTColorsV3.icon1.color : GTColorsV3.icon7.color;
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (widget.title != null) ...[
          Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              if (widget.titleCoinUrl!=null) ...[
                GTImage(
                  width: 20,
                  height: 20,
                  url: widget.titleCoinUrl,
                  radiusAll: 10,
                  showPlaceholder: true,
                  fit: BoxFit.cover,
                  errorPath: Images.icLendDefaultCoinIcon,
                  placeholderPath: Images.icLendDefaultCoinIcon,
                ),
                const SizedBox(
                  width: 4,
                ),
              ],
              Text(
                widget.title ?? "",
                style: gtTextStyleW400(fontSize: titleSize, color: GTColorsV3.text1.color),
              ),
              if (widget.showTitleMarket ?? false) ...[
                const SizedBox(
                  width: 4,
                ),
                Text(
                  "*",
                  style: gtTextStyleW400(fontSize: titleSize, color: GTColorsV3.color3.color),
                ),
              ]
            ],
          ),
          const SizedBox(
            height: 6,
          ),
        ],
        _container(),
        if (widget.bottomText != null) ...[
          const SizedBox(
            height: 6,
          ),
          Text(
            widget.bottomText ?? "",
            style: gtTextStyleW400(fontSize: 12, color: GTColorsV3.text1.color),
          ),
        ],
      ],
    );
  }

  Widget _container() {
    return Container(
      height: height,
      padding: const EdgeInsets.symmetric(horizontal: 12),
      decoration: BoxDecoration(
        color: (widget.isEnabled ?? true) ? GTColorsV3.bg1.color : GTColorsV3.cmpt2.color,
        borderRadius: BorderRadius.circular(radius),
        border: Border.all(
          color: GTColorsV3.divider5.color,
          width: GTDimensionsV3.strokeWidth,
        ),
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (widget.startWidget != null) ...[
            widget.startWidget!,
            SizedBox(
              width: space,
            )
          ],
          if (widget.startIcon != null) ...[
            Icon(
              widget.startIcon,
              color: iconColor,
              size: iconSize,
            ),
            SizedBox(
              width: space,
            )
          ],
          if (widget.fullWidth ?? true) ...[
            Expanded(
              child: Text(
                widget.text,
                style: gtTextStyleW500(fontSize: 14, color: widget.textColor ?? GTColorsV3.text3.color),
              ),
            ),
          ] else ...[
            Text(
              widget.text,
              style: gtTextStyleW500(fontSize: 14, color: widget.textColor ?? GTColorsV3.text3.color),
            ),
          ],
          SizedBox(
            width: space,
          ),
          if ((widget.endText ?? "").isNotEmpty) ...[
            Text(
              widget.endText ?? "",
              style: gtTextStyleW400(fontSize: 14, color: GTColorsV3.text1.color),
            ),
          ],
          Icon(
            widget.popupShowing ?? false ? GTIconFont.e9f8 : GTIconFont.e9f7,
            color: widget.popupShowing ?? false ? GTColorsV3.icon5.color : iconColor,
            size: iconSize,
          ),
        ],
      ),
    );
  }
}
